package orm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.sina.sae.util.SaeUserInfo;
import com.u2b.db.datasource.DbcpHandler;
import com.u2b.exceptions.DaoException;
import com.u2b.global.utils.CheckParameter;
import com.u2b.servlet.utils.ParameterParser;

public class AddProxy
{

    private Connection conn;
    private PreparedStatement ps;
    private DbcpHandler dbcpHandler;
    public AddProxy()
    {

        try
        {
        	dbcpHandler=DbcpHandler.getInstance();
        	conn=dbcpHandler.getWriteConnection();
        }
        catch (Exception e)
        {
            throw new RuntimeException("获得连接的时候出现异常", e);
        }
    }

   
    public int add(String sql, String[] keys, HashMap<String, Object> value)
    {
        int primaryKey = 0;
        try
        {
            ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            CheckParameter.checkParameterNumber(keys.length, ps);
            Object[] SQLvalues = new ParameterParser().getSQLValuesFromMap(keys, value);
            for (int i = 0; i < SQLvalues.length; i++)
            {
                ps.setObject(i + 1, SQLvalues[i]);
                System.out.println(this.getClass().getName() + "......." + keys[i] + "......." + SQLvalues[i] + ".......");
            }
            int flag = ps.executeUpdate();
            ResultSet rs = ps.getGeneratedKeys();
            if (rs.next())
            {
                primaryKey = rs.getInt(1);
            }

        }
        catch (SQLException e)
        {

            throw new DaoException(e.getMessage(), e);
        }
        finally
        {
             DbcpHandler.free(conn, ps, null);
        }
        return primaryKey;
    }
     
}
